package com.zb.test;

import org.junit.Test;

import java.time.*;


public class JAVA8_13_新时间与日期_本地时间与时间戳 {
    /**
     * LocalDate、LocalTime、LocalDateTime
     * 他们的实例都是不可变的对象，分别表示使用ISO-8601日历系统的日期、时间、日期和时间。
     * ISO-8601日历系统是国际标准化组织制定的现代公民的日期和时间的表示法
     */
    @Test
    public void test1(){
        //获取当前系统日期
        LocalDate localDate = LocalDate.now();
        System.out.println(localDate);

        //获取当前系统时间
        LocalTime localTime = LocalTime.now();
        System.out.println(localTime);

        //获取当前日期和时间
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime);

        //一些常用获取日期和时间的方法
        System.out.println("year:"+localDateTime.getYear());
        System.out.println("month:"+localDateTime.getMonthValue());
        System.out.println("day:"+localDateTime.getDayOfMonth());
        System.out.println("hour:"+localDateTime.getHour());
        System.out.println("minute:"+localDateTime.getMinute());
        System.out.println("second:"+localDateTime.getSecond());
        System.out.println("nano:"+localDateTime.getNano());//获取纳秒。1ms = 1000000ns
        System.out.println("mills:"+localDateTime.getNano()/1000000);
        System.out.println("week:"+localDateTime.getDayOfWeek());

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>");

        //设置指定的时间
        LocalDateTime localDateTime1 = LocalDateTime.of(2018,04,05,10,52,12);
        System.out.println(localDateTime1);

        LocalDateTime localDateTime2 = LocalDateTime.of(LocalDate.of(2018,4,5),LocalTime.of(10,52,12));
        System.out.println(localDateTime2);

    }

    /**
     * 对日期和时间的加减
     */
    @Test
    public void test2(){
        LocalDate localDate = LocalDate.now();
        LocalDate localDate1 = localDate.plusDays(5L);//在当前时间上加上5天 ，会产生一个新的实例
        System.out.println(localDate1);


        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        LocalDate localDate2 = localDate.minusYears(1L);//减去一年
        System.out.println(localDate2);

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        System.out.println(localDate);//对时间的加减不会对原来时间产生影响


    }

    /**
     * Instant 时间戳:
     * 一般是指 Unix 时间戳，即自从 Unix 纪元（格林威治时间 1970 年 1 月 1 日 00:00:00）到某个时间点的毫秒数。
     */
    @Test
    public void test3(){
        Instant instant = Instant.now();
        System.out.println(instant);//UTC 时间 ，中国处于+8区，所以会比当前时间慢8小时
        System.out.println(instant.toEpochMilli());//获取毫秒数，准确的毫秒数，与System.currentTimeMillis()获取到的相同
        System.out.println(System.currentTimeMillis());

        //获取准确的时间
        OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        System.out.println(offsetDateTime.toEpochSecond());//获取秒数

    }

    /**
     * Duration: 计算两个"时间"之间的间隔
     * Period: 计算两个"日期"之间的间隔
     */
    @Test
    public void test4() throws InterruptedException {
        Instant instant1 = Instant.now();
        Thread.sleep(1000);
        Instant instant2 = Instant.now();
        Duration duration = Duration.between(instant1,instant2);
        Long res = duration.toMillis();
        System.out.println(res);

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        LocalTime localTime1 = LocalTime.now();
        Thread.sleep(1200);
        LocalTime localTime2 = LocalTime.now();
        System.out.println(Duration.between(localTime1,localTime2).getSeconds());//毫秒转秒精度会受到影响

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");

        LocalDate localDate1 = LocalDate.of(2018,4,1);
        LocalDate localDate2 = LocalDate.now();
        Period period = Period.between(localDate1,localDate2);
        System.out.println(period.getDays());
    }

}
